package com.kamistoat.ruoyi.ruoyimodulessystem.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kamistoat.ruoyi.ruoyiapisystem.domain.SysDictData;
import com.kamistoat.ruoyi.ruoyiapisystem.entity.SysDictDataEntity;
import com.kamistoat.ruoyi.ruoyicommoncore.utils.StringUtils;
import com.kamistoat.ruoyi.ruoyicommoncore.utils.poi.ExcelUtil;
import com.kamistoat.ruoyi.ruoyicommoncore.web.controller.BaseController;
import com.kamistoat.ruoyi.ruoyicommoncore.web.domain.AjaxResult;
import com.kamistoat.ruoyi.ruoyicommoncore.web.page.TableDataInfo;
import com.kamistoat.ruoyi.ruoyicommonlog.annotation.Log;
import com.kamistoat.ruoyi.ruoyicommonlog.enums.BusinessType;
import com.kamistoat.ruoyi.ruoyicommonsecurity.annotation.RequiresPermissions;
import com.kamistoat.ruoyi.ruoyicommonsecurity.utils.SecurityUtils;
import com.kamistoat.ruoyi.ruoyimodulessystem.service.SysDictDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

import static com.kamistoat.ruoyi.ruoyicommoncore.utils.page.PageUtils.startPage;


/**
 * 字典数据表
 *
 * @author kamistoat
 * @email linjianshanke@163.com
 * @date 2022-08-09 10:34:46
 */
@RestController
@RequestMapping("/dict/data")
public class SysDictDataController extends BaseController {
    @Autowired
    private SysDictDataService sysDictDataService;

    /**
     * 匹配获取
     */
    @RequiresPermissions("system:dict:list")
    @GetMapping("/list")
    public TableDataInfo list(SysDictData sysDictData) {
        startPage();
        List<SysDictDataEntity> sysDictDataEntityList = sysDictDataService.selectDictDataList(
                StringUtils.isNull(sysDictData) ? new SysDictData() : sysDictData);
        return getDataTable(entity2Vo(sysDictDataEntityList));
    }

    /**
     * 导出字典数据
     */
    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    public void export(HttpServletResponse httpServletResponse, SysDictData sysDictData) {
        List<SysDictDataEntity> sysDictDataEntityList = sysDictDataService.selectDictDataList(sysDictData);
        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
        util.exportExcel(httpServletResponse, entity2Vo(sysDictDataEntityList), "字典数据");
    }

    /**
     * 查询字典数据详细
     */
    @RequiresPermissions("system:dict:query")
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable("dictCode") Long dictCode) {
        return AjaxResult.success(entity2Vo(
                sysDictDataService.getOne(new QueryWrapper<SysDictDataEntity>().eq("dict_code", dictCode))));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable("dictType") String dictType) {
        List<SysDictDataEntity> sysDictDataEntityList = sysDictDataService.selectDictDataByTypeAndCache(dictType);
        if (StringUtils.isNull(sysDictDataEntityList)) {
            sysDictDataEntityList = new ArrayList<SysDictDataEntity>();
        }
        return AjaxResult.success(sysDictDataEntityList);
    }

    /**
     * 新增字典数据
     */
    @RequiresPermissions("system:dict:add")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDictData sysDictData) {
        sysDictData.setCreateBy(SecurityUtils.getUserName());
        return toAjax(sysDictDataService.insertDictDataAndCache(vo2Entity(sysDictData)));
    }

    /**
     * 修改字典数据
     */
    @RequiresPermissions("system:dict:edit")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDictData sysDictData) {
        sysDictData.setUpdateBy(SecurityUtils.getUserName());
        return toAjax(sysDictDataService.updateDictDataAndCache(vo2Entity(sysDictData)));
    }

    /**
     * 删除字典类型
     */
    @RequiresPermissions("system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public AjaxResult remove(@PathVariable("dictCodes") Long[] dictCodes) {
        sysDictDataService.deleteDictDataByIdsAndRemoveCache(dictCodes);
        return success();
    }


    /**
     * Entity 和 Vo类的转换
     */
    private List<SysDictData> entity2Vo(List<SysDictDataEntity> sysDictDataEntityList) {
        return
                sysDictDataEntityList.stream().map(sysDictDataEntity -> {
                    SysDictData sysDictData = new SysDictData();
                    BeanUtils.copyProperties(sysDictDataEntity, sysDictData);
                    return sysDictData;
                }).collect(Collectors.toList());
    }

    private SysDictData entity2Vo(SysDictDataEntity sysDictDataEntity) {
        SysDictData sysDictData = new SysDictData();
        BeanUtils.copyProperties(sysDictDataEntity, sysDictData);
        return sysDictData;
    }

    private List<SysDictDataEntity> vo2Entity(List<SysDictData> sysConfigList) {
        return
                sysConfigList.stream().map(sysDictData -> {
                    SysDictDataEntity sysDictDataEntity = new SysDictDataEntity();
                    BeanUtils.copyProperties(sysDictData, sysDictDataEntity);
                    return sysDictDataEntity;
                }).collect(Collectors.toList());
    }

    private SysDictDataEntity vo2Entity(SysDictData sysDictData) {
        SysDictDataEntity sysDictDataEntity = new SysDictDataEntity();
        BeanUtils.copyProperties(sysDictData, sysDictDataEntity);
        return sysDictDataEntity;
    }

}
